package com.smforj.ssm.frame.core.socket;

import com.smforj.ssm.util.PropKit;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

public class SocketServiceImpl implements SocketService {

    private String hostName = "192.168.4.104";
    private int hostPort = 28081;
    private static SocketAddress socketAddress;
    private static Socket socket;
    private int i = 0;

    public SocketServiceImpl() {
        try {
            socketAddress = new InetSocketAddress(PropKit.use("smforj.properties").get("socketHostName"), Integer.parseInt(PropKit.get("socketHostPort")));
            socket = new Socket();
            socket.connect(socketAddress, 3000);
            sendAndRead(String.valueOf((i + 1)));
        } catch (Exception e) {
            System.out.print("连接失败" + e.getMessage());
        }
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public int getHostPort() {
        return hostPort;
    }

    public void setHostPort(int hostPort) {
        this.hostPort = hostPort;
    }

    private void connect() {
        if (socket != null && socketAddress != null) {
            //与服务端建立连接
            try {
                socket.connect(socketAddress, 3000);
                System.out.println("connect...");
            } catch (UnknownHostException e) {
                e.printStackTrace();
                socket = null;
            } catch (IOException e) {
                e.printStackTrace();
                socket = null;
            }
        }
    }

    @Override
    public String sendAndRead(String sendMsg) {
        System.out.println("send" + sendMsg);
        String rMsg = "";
        if (socket == null)
            return rMsg;
        if (socket != null && !socket.isConnected())
            connect();
        if (socket != null && socket.isConnected()) {
            try {
                //由Socket对象得到输出流，并构造PrintWriter对象
                BufferedWriter os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                String readline = sendMsg; //从系统标准输入读入一字符串
                //将从系统标准输入读入的字符串输出到Server
                os.write(readline);
                //刷新输出流，使Server马上收到该字符串
                os.flush();
                //由Socket对象得到输入流，并构造相应的BufferedReader对象
                BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
                //最多等待10秒读取时间
                socket.setSoTimeout(3000);
                //读取消息
                rMsg = is.readLine();
//                rMsg = rMsg.substring(27);
//                os.close(); //关闭Socket输出流
//                is.close(); //关闭Socket输入流
//                os = null;
//                is = null;
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
                rMsg = "";
            } finally {
//                try {
//                    socket.close();
//                } catch (Exception ex) {
//                    System.out.println(ex.getMessage());
//                }
            }
        }
        System.out.println("response" + rMsg);
        return rMsg;
    }

    @Override
    public String getAddress() {
        return null;
    }

}
